//
//  UICollectionViewMultiFlowLayout.m
//  Systetagram
//
//  Created by 欧杨 on 2021/1/1.
//

#import "UICollectionViewMultiFlowLayout.h"

@interface UICollectionViewMultiFlowLayout()

@end

@implementation UICollectionViewMultiFlowLayout


- (void)prepareLayout
{
    [super prepareLayout];
    self.minimumInteritemSpacing = 5;
    self.minimumLineSpacing = 5;
    //self.headerReferenceSize = CGSizeZero;
    //self.footerReferenceSize = CGSizeZero;
    
    /*
     *设置section的四条边到item的margin
     */
    self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    if (!_layoutAttributes) {
        [self computeAttributes:2];
    }
    
    //这句有问题
    //[self setEstimatedItemSize:self.estimatedSize];
}

- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
//    NSMutableArray *layouts = [NSMutableArray array];
//    for (UICollectionViewLayoutAttributes *attr in self.layoutAttributes) {
//        if (CGRectIntersectsRect(attr.frame, rect)) {
//            [layouts addObject:attr];
//        }
//    }
//    return layouts;
    return self.layoutAttributes;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return self.layoutAttributes[indexPath.item];
}

- (CGSize)collectionViewContentSize
{
    //return self.contentBounds.size;
    return self.contentSize;
}

- (void)computeAttributes:(NSUInteger)columnNumbers
{
    self.contentBounds = CGRectMake(0, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    CGFloat contentWidth = self.collectionView.bounds.size.width - self.sectionInset.left - self.sectionInset.right;
    CGFloat itemWidth = (contentWidth - self.minimumInteritemSpacing * (columnNumbers - 1)) / columnNumbers;
    
    NSMutableArray<NSNumber *> *columnsHeights = [NSMutableArray array];
    for (int i=0; i<columnNumbers; i++) {
        [columnsHeights addObject:@(self.sectionInset.top)];
    }
    
    NSUInteger columnItemCount[columnNumbers];
    //NSMutableArray<NSNumber *> *columnItemCount = [NSMutableArray array];
    for (NSUInteger i=0; i<columnNumbers; i++) {
        //[columnsHeights addObject:@(0)];
        columnItemCount[i] = 0;
    }
    
    NSMutableArray<UICollectionViewLayoutAttributes *> *attributes = [NSMutableArray array];
    
    NSUInteger row = 0;
    for (blogModel *item in self.items) {
        //创建一个attributes
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:0];
        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        //找出最短的column
        NSNumber *minHeight = [columnsHeights valueForKeyPath:@"@min.self"];
        NSUInteger minHeightColumn = [columnsHeights indexOfObject:minHeight];
        
        columnItemCount[minHeightColumn]++;
        CGFloat itemX = (itemWidth + self.minimumInteritemSpacing) * minHeightColumn + self.sectionInset.left;
        CGFloat itemY = [minHeight doubleValue];
        CGFloat itemHeight = [item.picture_height doubleValue] * (itemWidth / [item.picture_width doubleValue]);
        
        [attribute setFrame:CGRectMake(itemX, itemY, itemWidth, itemHeight)];
        [attributes addObject:attribute];
        self.contentBounds = CGRectUnion(self.contentBounds, attribute.frame);
        
        columnsHeights[minHeightColumn] = [NSNumber numberWithDouble:([columnsHeights[minHeightColumn] doubleValue] + itemHeight + self.minimumLineSpacing)];
        
        row++;
    }
    
    NSNumber *maxHeight = [columnsHeights valueForKeyPath:@"@max.self"];
    NSUInteger maxHeightColumn = [columnsHeights indexOfObject:maxHeight];
    CGFloat itemHeight = ([maxHeight doubleValue] - self.minimumLineSpacing * columnItemCount[maxHeightColumn]) / columnItemCount[maxHeightColumn];
    
    self.estimatedSize = CGSizeMake(itemWidth, itemHeight);
    self.layoutAttributes = [NSArray arrayWithArray:attributes];
    self.contentSize = CGSizeMake(self.collectionView.bounds.size.width, [maxHeight doubleValue]);
}

@end
